<%--
  Created by IntelliJ IDEA.
  User:  Jun
  Date: 2018/8/15
  Time: 16:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="pu" uri="http://pageUtil.cn/common/" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ page import="cv.gzd.robot.pojo.User" %>
<%
    User user = (User) session.getAttribute("USER_SESSION");
%>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <!-- Bootstrap Core CSS -->
    <link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">

    <!-- MetisMenu CSS -->
    <link href="${pageContext.request.contextPath}/css/metisMenu.min.css" rel="stylesheet">

    <!-- DataTables CSS -->
    <link href="${pageContext.request.contextPath}/css/dataTables.bootstrap.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="${pageContext.request.contextPath}/css/sb-admin-2.css" rel="stylesheet">

    <!-- Custom Fonts -->
    <link href="${pageContext.request.contextPath}/css/font-awesome.min.css" rel="stylesheet"
          type="text/css">
    <link href="${pageContext.request.contextPath}/css/boot-crm.css" rel="stylesheet"
          type="text/css">

    <!--<script-->
    <script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
    <script type="text/javascript">
        window.onload = function () {
            var height = $(window).height();
            $("#wrapper").css("height", height);
        }
    </script>
</head>
<body>
<div id="wrapper" style="background-color:#000000;">
    <!-- Navigation -->
    <nav class="navbar navbar-default navbar-static-top" role="navigation"
         style="margin-bottom: 0;background-color: #01C9E2">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target=".navbar-collapse">
                <span class="sr-only">Toggle navigation</span> <span
                    class="icon-bar"></span> <span class="icon-bar"></span> <span
                    class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/index.action">IRIM后端控制管理系统</a>
        </div>

        <ul class="nav navbar-top-links navbar-right">
            <li>
                <ul><%=user.getRealname()%><%=user.getAuthorization()%>,你好!</ul>
            </li>
            <li class="dropdown"><a class="dropdown-toggle"
                                    data-toggle="dropdown" href="#"> <i class="fa fa-user fa-fw"></i>
                <i class="fa fa-caret-down"></i>
            </a>
                <ul class="dropdown-menu dropdown-user">
                    <li><a href="#" data-toggle="modal" data-target="#PswdSaveDialog"><i
                            class="fa fa-user fa-fw"></i> 修改密码</a></li>
                    <li class="divider"></li>
                    <li><a href="/logout"><i class="fa fa-sign-out fa-fw"></i>
                        退出登录</a></li>
                </ul> <!-- /.dropdown-User --></li>
        </ul>

    </nav>
    <div class="navdiv collapse navbar-collapse">
        <ul class="nav navbar-nav ">
            <li class="">
                <div><a href="/index.action"><span class="icon fa fa-gamepad"></span>状态和控制</a></div>
            </li>
            <li class="">
                <div><a href="/log/list.action"><span class="icon fa fa-wpforms"></span> 运行日志</a></div>
            </li>
            <li class="">
                <div><a href="/dev/list.action"><span class="icon fa fa-desktop"></span>设备管理</a></div>
            </li>
            <li class="">
                <div><a href="/user/list.action"><span class="icon fa fa-users"></span>用户管理</a></div>
            </li>
        </ul>
    </div>

    <div class="panel panel-default page-wapper">
        <div class=" headingcolor">
            <button type="button" class="addStyle btn btn-primary  " data-toggle="modal"
                    data-target="#UserSaveDialog">新增用户
            </button>
            <form class="form-inline" action="${pageContext.request.contextPath }/user/list.action"
                  method="post">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" class="form-control" id="username" value="${username}" name="username">
                </div>
                <div class="form-group">
                    <label for="authorization">权限</label>
                    <select class="form-control" id="authorization" placeholder="权限" name="authorization">
                        <option value="">--请选择--</option>
                        <c:forEach items="${Typeauthorization}" var="item">
                            <option value="${item.authorization}"
                                    <c:if test="${item.authorization == authorization}"> selected</c:if>>${item.authorization }</option>
                        </c:forEach>
                    </select>
                </div>
                <div class="form-group">
                    <label for="realname">姓名</label>
                    <input type="text" class="form-control" id="realname" value="${realname }" name="realname">
                </div>
                <button type="submit" class="btn btn-primary">查询</button>
            </form>
        </div>


        <div class="panel-body">
            <div class="row">
                <div class="col-lg-12">
                    <table class="table table-bordered table-striped tablestyle">
                        <thead class="thcolor">
                        <tr>
                            <th>用户id</th>
                            <th>用户名</th>
                            <th>密码</th>
                            <th>角色权限</th>
                            <th>姓名</th>
                            <th>部门</th>
                            <th>联系电话</th>
                            <th>邮箱</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach items="${page.rows}" var="row">
                            <tr>
                                <td>${row.uid}</td>
                                <td>${row.username}</td>
                                <td>${row.password}</td>
                                <td>${row.authorization}</td>
                                <td>${row.realname}</td>
                                <td>${row.dept}</td>
                                <td>${row.phone}</td>
                                <td>${row.email}</td>
                                <td>
                                    <a href="#" class="btn btn-primary btn-xs" data-toggle="modal"
                                       data-target="#UserEditDialog" onclick="editUser(${row.uid})">修改</a>
                                    <a href="#" class="btn btn-danger btn-xs"
                                       onclick="deleteUser(${row.uid})">删除</a>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                    <div class="col-md-12 text-right">
                        <pu:page url="${pageContext.request.contextPath }/user/list.action"/>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>
<!--表格-->

<!--编辑界面-->
<div class="modal fade" id="UserEditDialog" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">修改用户信息</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="edit_User_form">
                    <input type="hidden" id="edit_Uid" name="uid"/>
                    <div class="form-group">
                        <label for="edit_Username" class="col-sm-2 control-label">用户名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edit_Username" placeholder="用户名"
                                   name="username" readonly="readonly">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="edit_Pswd" class="col-sm-2 control-label">密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="edit_Pswd" placeholder="密码"
                                   name="password">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="edit_Authorization" style="float:left;padding:7px 15px 0 27px;">角色权限</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="edit_Authorization" placeholder="角色权限"
                                    name="authorization">
                                <option value="">--请选择--</option>
                                <c:forEach items="${Typeauthorization}" var="item">
                                    <option value="${item.authorization}"
                                            <c:if test="${item.aid == authorization}"> selected</c:if>>${item.authorization }</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edit_Realname" class="col-sm-2 control-label">姓名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edit_Realname" placeholder="姓名"
                                   name="realname">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="edit_Dept" class="col-sm-2 control-label">部门</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edit_Dept" placeholder="部门"
                                   name="dept">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="edit_mobile" class="col-sm-2 control-label">联系电话</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edit_mobile" placeholder="联系电话"
                                   name="phone">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="edit_Email" class="col-sm-2 control-label">邮箱</label>
                        <div class="col-sm-10">
                            <input type="email" class="form-control" id="edit_Email" placeholder="邮箱"
                                   name="Email">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="updateUser()">保存修改</button>
            </div>
        </div>
    </div>
</div>
<!-- /#wrapper -->
<%--新增界面--%>
<div class="modal fade" id="UserSaveDialog" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="UserLabel">新增用户信息</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="save_User_form">
                    <div class="form-group">
                        <label for="save_Username" class="col-sm-2 control-label">用户名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="save_Username" placeholder="用户名"
                                   name="username">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="save_Pswd" class="col-sm-2 control-label">密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="save_Pswd" placeholder="密码"
                                   name="password">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="save_Authorization" style="float:left;padding:7px 15px 0 27px;">角色权限</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="save_Authorization" placeholder="角色权限"
                                    name="authorization">
                                <option value="">--请选择--</option>
                                <c:forEach items="${Typeauthorization}" var="item">
                                    <option value="${item.authorization}"
                                            <c:if test="${item.aid == authorization}"> selected</c:if>>${item.authorization }</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="save_Realname" class="col-sm-2 control-label">姓名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="save_Realname" placeholder="姓名"
                                   name="realname">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="save_Dept" class="col-sm-2 control-label">部门</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="save_Dept" placeholder="部门"
                                   name="dept">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="save_mobile" class="col-sm-2 control-label">联系电话</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="save_mobile" placeholder="联系电话"
                                   name="phone">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="save_Email" class="col-sm-2 control-label">邮箱</label>
                        <div class="col-sm-10">
                            <input type="email" class="form-control" id="save_Email" placeholder="邮箱"
                                   name="Email">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="insertUser()">保存</button>
            </div>
        </div>
    </div>
</div>
<%--修改密码--%>
<div class="modal fade" id="PswdSaveDialog" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="SavePswd">修改密码</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="save_Pswd_form">
                    <input type="hidden" id="edit_Pswd_Uid" name="uid" value="<%=user.getUid()%>"/>
                    <div class="form-group">
                        <label for="edit_Pswd_new" class="col-sm-2 control-label">新密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="edit_Pswd_new" placeholder="新密码"
                                   name="password">
                        </div>
                        <label for="edit_Pswd_again" class="col-sm-2 control-label">再次输入新密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="edit_Pswd_again" placeholder="再次输入新密码"
                                   name="pwsd">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="updatePswd()">保存修改</button>
            </div>
        </div>
    </div>
</div>
<!-- jQuery -->
<script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>

<!-- Metis Menu Plugin JavaScript -->
<script src="${pageContext.request.contextPath}/js/metisMenu.min.js"></script>

<!-- DataTables JavaScript -    ->
<script src="../js/jquery.dataTables.min.js"></script>
<script src="../js/dataTables.bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="${pageContext.request.contextPath}/js/sb-admin-2.js"></script>

<script type="text/javascript">
    function editUser(id) {
        $.ajax({
            type: "get",
            url: "${pageContext.request.contextPath}/user/edit.action",
            data: {"id": id},
            success: function (data) {
                $("#edit_Uid").val(data.uid);
                $("#edit_Username").val(data.username);
                $("#edit_Pswd").val(data.password)
                $("#edit_Authorization").val(data.authorization)
                $("#edit_Realname").val(data.realname)
                $("#edit_Dept").val(data.dept);
                $("#edit_mobile").val(data.phone);
                $("#edit_Email").val(data.Email);
            }
        });
    }

    function insertUser() {
        $.post("${pageContext.request.contextPath}/user/save.action", $("#save_User_form").serialize(), function (data) {
            if(data.code==801){
                alert("用户信息添加成功！");
                window.location.reload();
            }
            if(data.code==802){
                alert("该用户名已存在!");
                // window.location.reload();
            }
        });
    }

    function updateUser() {
        $.post("${pageContext.request.contextPath}/user/update.action", $("#edit_User_form").serialize(), function (data) {
            if(data.code==801){
                alert("客户信息更新成功！");
                window.location.reload();
            }
        });
    }

    function deleteUser(id) {
        if (confirm('确实要删除该客户吗?')) {
            $.post("${pageContext.request.contextPath}/user/delete.action", {"id": id}, function (data) {
                if(data.code==801){
                    alert("客户删除更新成功！");
                    window.location.reload();
                }
            });
        }
    }


    function updatePswd() {
        $.post("${pageContext.request.contextPath}/user/updatepswd.action", $("#save_Pswd_form").serialize(), function (data) {
            if (data.code==801){
                alert("修改密码成功！");
                window.location.reload();
            }
            if(data.code==802){
                alert("两次密码不一致！");
                window.location.reload();
            }
        });
    }

</script>

</body>

</html>